<?php

namespace App\Listeners\Prize;

use App\Events\EntityPrizeCreated;
use App\Morefun\Prize\EntityCodeSynchronizer;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Ramsey\Uuid\Uuid;

class GeneratePrizeCode
{
    protected $synchronizer;

    public function __construct(EntityCodeSynchronizer $synchronizer)
    {
        $this->synchronizer = $synchronizer;
    }

    public function handle(EntityPrizeCreated $event)
    {
        if ($event->getNumberOfBound() == 0 || !Uuid::isValid($event->getPrizeId())) {
            return;
        }

        $codes = $this->synchronizer->generateCodes($event->getNumberOfBound());
        $prizeId = $event->getPrizeId();
        $this->synchronizer->save($prizeId, $codes);
        $this->synchronizer->writeToRedis($prizeId, $codes);
    }
}
